package com.smxy.mall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smxy.mall.common.CustomException;
import com.smxy.mall.common.Response;
import com.smxy.mall.dao.UserDao;
import com.smxy.mall.entity.User;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.user.DetailReq;
import com.smxy.mall.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 账号密码注册
     * @param userName
     * @param psw
     * @return
     */
    @Override
    public User saveFromUserName(String userName, String psw, Integer rolesId) {
        User obj = userDao.findByUserName(userName);
        if(obj!=null){
            throw new CustomException(Response.fail("用户名"+userName+"已存在"));
        }
        String md5Psw = DigestUtils.md5DigestAsHex(psw.getBytes());
        User user = new User();
        user.setUserName(userName);
        user.setUserPsw(md5Psw);
        user.setType("admin");
        user.setRolesId(rolesId);
        return userDao.save(user);
    }

    /**
     * 账号密码登录
     * @param userName
     * @param userPsw
     * @return
     */
    @Override
    public User findByUserNameAndUserPsw(String userName, String userPsw) {
        String md5Psw = DigestUtils.md5DigestAsHex(userPsw.getBytes());
        return userDao.findByUserNameAndUserPsw(userName,md5Psw);
    }

    /**
     * 手机号密码注册
     * @param phone
     * @param psw
     * @return
     */
    @Override
    public User saveFromPhone(String phone, String psw) {
        User obj = userDao.findByPhone(phone); //根据手机号查询用户
        if(obj!=null){
            throw new CustomException(Response.fail("手机号"+phone+"已存在"));
        }
        String md5Psw = DigestUtils.md5DigestAsHex(psw.getBytes());
        User user = new User();
        user.setPhone(phone);
        user.setUserPsw(md5Psw);
        user.setType("app");
        return userDao.save(user);
    }

    /**
     * 手机号密码登录
     * @param phone
     * @param userPsw
     * @return
     */
    @Override
    public User findByPhoneAndUserPsw(String phone, String userPsw) {
        String md5Psw = DigestUtils.md5DigestAsHex(userPsw.getBytes());
        return userDao.findByPhoneAndUserPsw(phone,md5Psw);
    }

    /**
     * 用户列表 分页
     * @param type app 普通用户  admin 后台pc用户
     * @param pageable
     * @return
     */
    @Override
    public Page<User> listByType(String type, Pageable pageable) {
        return userDao.findByType(type,pageable);
    }

    @Override
    public User save(User user) {
        return userDao.save(user);
    }

    @Override
    public User wxSave(JSONObject obj) {
        User user = new User();
        System.out.println(obj);
        user.setOpenId(obj.get("openId").toString());
        user.setImg(obj.get("avatarUrl").toString());
        user.setName(obj.get("nickName").toString());
        user.setSex(obj.get("gender").toString());
        return userDao.save(user);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public User updateRoles(String roles, Integer userId) {
        User user = userDao.findById(userId);
        if(user==null){
            throw new CustomException(Response.fail("用户id不存在"));
        }
        user.setRoles(roles);
        return userDao.save(user);
    }

    @Override
    public User findByPhone(String phone) {
        return userDao.findByPhone(phone);
    }

    @Override
    public User findByOpenId(String openId) {
        return userDao.findByOpenId(openId);
    }

    @Override
    public Page<User> list(Pageable pageable) {
        return userDao.findAll(pageable);
    }

    @Override
    public User userEdit(String sex, Current current) {
        User user = new User();
        user.setId(current.getUserId());
        user.setSex(sex);
        return userDao.save(user);
    }

    @Override
    public Map<String, Object> userCenter(Integer id) {
        Map<String,Object> map = new HashMap<>();
        User userInfo = userDao.findById(id);
        Map<String,Integer> order = new HashMap<>();
        order.put("payment",1);
        order.put("delivery",2);
        order.put("received",1);
        order.put("comment",1);
        order.put("cancel",3);
        map.put("userInfo",userInfo);
        map.put("order",order);
        map.put("coupon",3);
        return map;
    }

    @Override
    public List<Object> getRoutes(Integer id) {
        String str = "[{'name':'ComponentDemo','children':[{'name':'TinymceDemo'},{'name':'MarkdownDemo'}]},{'name':'User','children':[{'name':'UserList'}]}]";
        System.out.println(str);
        Gson gson=new Gson();
        List<Object> routes= gson.fromJson(str, new TypeToken<List<Object>>() {}.getType());
        System.out.println(routes);
        return routes;
    }

    @Override
    public User edit(DetailReq detailReq) {
        User user = new User();
        user.setId(detailReq.getId());
        user.setUserName(detailReq.getUserName());
        user.setPhone(detailReq.getPhone());
        user.setType(detailReq.getType());
        user.setRolesId(detailReq.getRolesId());
        user.setName(detailReq.getName());
        return userDao.save(user);
    }
}
